package tictactoe.GUI;

//~--- non-JDK imports --------------------------------------------------------

import tictactoe.Player;

import tictactoe.exceptions.network.NetworkException;

import tictactoe.multiplayer.client.Client;

//~--- JDK imports ------------------------------------------------------------

import java.awt.Point;

import java.util.Set;

import javax.naming.event.EventDirContext;

import javax.swing.JDialog;
import javax.swing.JOptionPane;

/**
 *
 * @author Home
 */
public class ClientImpl extends Client {
    private GameWindow     gameWin;
    private NewMultiplayer multiplayerWin;

    /**
     *
     * @param server
     * @param dialog
     * @throws NetworkException
     */
    public ClientImpl(String server, NewMultiplayer dialog) throws NetworkException {
        super(server);
        multiplayerWin = dialog;
    }

    /**
     *
     * @param dialog
     */
    public void setGameWindow(GameWindow dialog) {
        gameWin        = dialog;
        multiplayerWin = null;
    }

    /**
     *
     * @return
     */
    public JDialog getActiveWin() {
        if (multiplayerWin != null) {
            return multiplayerWin;
        }

        if (gameWin != null) {
            return gameWin;
        }

        throw new IllegalStateException("no dialogs set!");
    }

    /**
     *
     */
    @Override
    public void connectionAcknowledged() {

        // ok
    }

    /**
     *
     * @param message
     */
    @Override
    public void exceptionReceived(String message) {
        JOptionPane.showMessageDialog(getActiveWin(), message);
    }

    /**
     *
     * @param move
     */
    @Override
    public void moveReceived(Point move) {
        if (gameWin != null) {
            gameWin.remoteTurn(move);
        }
    }

    /**
     *
     */
    @Override
    public void oponentDisconnected() {
        JOptionPane.showMessageDialog(getActiveWin(), "Холболт саллаа. ");
    }

    /**
     *
     * @param from
     */
    @Override
    public void playRequestReceived(Player from) {
        if (multiplayerWin != null) {
            int choice = JOptionPane.showConfirmDialog(multiplayerWin,
                             "Тоглогч " + from.getName() + " таньтай тоглохыг уриж байна", "Хүсэлт",
                             JOptionPane.YES_NO_OPTION);

            if (choice == JOptionPane.YES_OPTION) {
                acceptChallenge(from);
                multiplayerWin.startGame(from, getPlayer());
            }
        }
    }

    /**
     *
     * @param players
     */
    @Override
    public void playersSetReceived(Set<Player> players) {
        if (multiplayerWin != null) {
            multiplayerWin.playersSetReceived(players);
        }
    }

    /**
     *
     * @param msg
     */
    @Override
    public void receivedMessage(String msg) {
        System.out.println(msg);

        // ok
    }

    /**
     *
     * @param from
     */
    @Override
    public void requestAccepted(Player from) {
        if (multiplayerWin != null) {
            multiplayerWin.startGame(getPlayer(), from);
        }
    }

    /**
     *
     */
    @Override
    public void serverShuttingDown() {
        JOptionPane.showMessageDialog(getActiveWin(), "Сервер унтарлаа, Танд баярлалаа. ");
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
